// https://vuex.vuejs.org/zh-cn/intro.html
// make sure to call Vue.use(Vuex) if using a module system
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    url: 'https://fhxxg.fanglilai.cn/',
    cookie: '',
    nick_name: '',
    avatar: '',
    userList: [],
    userInfo: {},
    // tenant
    date: [],
    bids: '',
    renterPhone: '',
    bookingTime: '',
    openid: '',
    // hardware
    district_name: '',
    district_id: '',
    meterStatusSuccess: false,
    eleWaterStatus: '',
    address: '',
    // sector
    sectorItem: {},
    staffItem: {}
  },
  mutations: {
    setBuildingListAddOne: (state, item) => {
      const obj = state
      obj.buildingList.push(item)
    },
    setBuildingListReplaceIndex: (state, index, buildingItem) => {
      const obj = state
      console.log(index)
      console.log(buildingItem)
      obj.buildingList.splice(index, 1)
      obj.buildingList.splice(index, 0, buildingItem)
    },
    setStaffItem: (state, staffItem) => {
      const obj = state
      obj.staffItem = {}
      obj.staffItem = staffItem
    },
    setSectorItem: (state, sector) => {
      const obj = state
      obj.sectorItem = {}
      obj.sectorItem = sector
    },
    setNickName: (state, name) => {
      const obj = state
      obj.nick_name = name
    },
    setAvatar: (state, avatar) => {
      const obj = state
      obj.avatar = avatar
    },
    setCookie: (state, cookie) => {
      const obj = state
      obj.cookie = cookie
    },
    setUserInfo: (state, userInfo) => {
      const obj = state
      obj.userInfo = userInfo
    },
    setUser: (state, user) => {
      const obj = state
      if (obj.userList.length === 0) {
        obj.userList.push(user)
      } else {
        var isNewAcc = 0
        for (let index in obj.userList) {
          if (obj.userList[index].account === user.account) {
            isNewAcc++
          }
        }
        if (isNewAcc === 0) {
          obj.userList.push(user)
        }
      }
    },
    setDistrictName: (state, name) => {
      const obj = state
      obj.district_name = name
    },
    setDistrictId: (state, name) => {
      const obj = state
      obj.district_id = name
    },
    setMeterStatusSuccess: (state, status) => {
      const obj = state
      obj.meterStatusSuccess = status
    },
    setEleWaterStatus: (state, status) => {
      const obj = state
      obj.eleWaterStatus = status
    },
    setAddress: (state, status) => {
      const obj = state
      obj.address = status
    },
    setDate: (state, date) => {
      const obj = state
      obj.date = date
    },
    setBids: (state, bids) => {
      const obj = state
      obj.bids = bids
    },
    setRenterPhone: (state, renterPhone) => {
      const obj = state
      obj.renterPhone = renterPhone
    },
    setBookingTime: (state, bookingTime) => {
      const obj = state
      obj.bookingTime = bookingTime
    },
    setOpenid: (state, openid) => {
      const obj = state
      obj.openid = openid
    }
  }
})

export default store
